package com.falcon.web.security;

import com.falcon.core.codec.IPasswordEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/** 自定义用户密码加密算法
 * 参考： BCryptPasswordEncoder, 这个需要额外配置，因为创建用户的时候，会用
 * @author zongf
 * @date 2025-02-18
 **/
@Slf4j
@Component
public class SecurityPasswordEncoder implements PasswordEncoder {

    @Autowired
    private IPasswordEncoder userPasswordEncoder;

    @Override
    public String encode(CharSequence rawPassword) {
        return userPasswordEncoder.encode(rawPassword.toString());
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        if(rawPassword == null || rawPassword.length() == 0
            || encodedPassword == null || encodedPassword.length() == 0){
            return false;
        }
        return userPasswordEncoder.encode(rawPassword.toString()).equals(encodedPassword);
    }
}
